package com.bxd.baodan.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.ManualAddInsuranceBean;
import com.bxd.baodan.util.AppManager;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.IosDialog;
import com.umeng.analytics.MobclickAgent;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ManualAdd3Activity extends BaseActivity implements View.OnClickListener {

    private TextView mTvCancel;
    private RelativeLayout mRlSave;
    private TextView mTvBirthday;
    private EditText mEtMoney;
    private EditText mEtUserName;

    private TimePickerView birthdayPicker;

    private ManualAddInsuranceBean mBean;
    private RadioGroup mRadioGroup;

    private String needEvaluation = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_add3);
        hideBar();
        initView();

        mBean = (ManualAddInsuranceBean) getIntent().getSerializableExtra("bean");
    }

    private void initView() {
        mTvCancel = getTextView(R.id.tv_cancel);
        mTvCancel.setOnClickListener(this);
        mRlSave = getRelativeLayout(R.id.rl_save);
        mRlSave.setOnClickListener(this);
        mTvBirthday = getTextView(R.id.tv_birthday);
        mTvBirthday.setOnClickListener(this);
        mEtMoney = getEditText(R.id.et_money);
        mEtUserName = getEditText(R.id.et_user_name);
        mRadioGroup = (RadioGroup) findViewById(R.id.radio_group);


        birthdayPicker = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                mTvBirthday.setText(getTime(date));
                mTvBirthday.setTextColor(Color.parseColor("#333333"));
            }
        })
                .setTitleText("选择出生日期")
                .setType(TimePickerView.Type.YEAR_MONTH_DAY)
                .setLabel("年", "月", "日", "", "", "") //设置空字符串以隐藏单位提示   hide label
                .setDividerColor(Color.GRAY)
                .setContentSize(20)
                .build();


        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_button_1:
                        MobclickAgent.onEvent(App.getContext(), "v6_0_0_SY_bdcp_ckxq_xycp");
                        needEvaluation = "1";
                        break;
                    case R.id.radio_button_2:
                        needEvaluation = "0";
                        break;
                }
            }
        });
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                ManualAdd3Activity.this.finish();
                break;

            case R.id.tv_birthday:
                // 生日
                birthdayPicker.show();
                break;
            case R.id.rl_save:


                String money = getString(mEtMoney);
                if (TextUtils.isEmpty(money)) {
                    showToast("请输入保费金额！");
                    return;
                }
                String userName = getString(mEtUserName);
                if (TextUtils.isEmpty(userName)) {
                    showToast("请输入被保人姓名！");
                    return;
                }

                String birthday = getString(mTvBirthday);
                if (TextUtils.isEmpty(birthday) || "请选择出生日期".equals(birthday)) {
                    showToast("请选择被保人出生日期！");
                    return;
                }

                if (TextUtils.isEmpty(needEvaluation)) {
                    showToast("请选择是否需要精算师为您做保单测评！");
                    return;
                }

                // 保存
                if (mBean == null) {
                    showToast("数据错误，请返回上一步重试！");
                    return;
                }
                mBean.insureFee = money;
                mBean.name = userName;
                mBean.birthday = birthday;
                mBean.needEvaluation = needEvaluation;
                mBean.processWay = "1";// 手动添加

                Bundle b = new Bundle();
                b.putSerializable("bean", mBean);

                NetUtil netUtil = new NetUtil(ManualAdd3Activity.this, JsonApi.INSURANCE_ADD);
                netUtil.setParams("catId", mBean.catId);
                netUtil.setParams("companyId", mBean.companyId);
                netUtil.setParams("productName", mBean.productName);
                netUtil.setParams("insureStartDate", mBean.insureStartDate);
                netUtil.setParams("insureEndDate", mBean.insureEndDate);
                netUtil.setParams("insureFee", mBean.insureFee);
                netUtil.setParams("name", mBean.name);
                netUtil.setParams("birthday", mBean.birthday);
                netUtil.setParams("needEvaluation", mBean.needEvaluation);
                netUtil.setParams("renewRemind", mBean.renewRemind);
                netUtil.setParams("payMethod", mBean.payMethod);
                netUtil.setParams("processWay ", "1");

                netUtil.postRequest("正在提交数据...", new NetUtil.RequestStringListener() {
                    @Override
                    public void onComplete(String result) {

                        // TODO: 18/1/15
                        showExitDialog("保单上传成功!",
                                "规划顾问正在为保单评测~");

                    }
                });
                break;
        }
    }


    public void showExitDialog(String title, String content) {
        new IosDialog(this).builder().setCancelable(false)
                .setTitle(title)
                .setMsg(content)
                .setNegativeButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AppManager.getInstance().finishOtherActivity(MainActivity.class);

                    }
                }).show();
    }
}
